home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Memory / PlatfMem.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  5.1 KB  |  187 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PlatfMem.cpp
  3.  
  4.     Contains:    Platform layer implementation
  5.  
  6.     Owned by:    Michael Burbidge
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.     
  12.          <5>     8/13/96    RA        1376299: ES crashes in 24-bit mode. We
  13.                                     again call StripAddress in
  14.                                     PlatformAllocateBlock
  15.          <4>     7/31/96    CSL        Fix to previous checking--needed to include
  16.                                     MemDebg.h
  17.          <3>     7/31/96    DH        #1372007: Store handle inside blocks
  18.                                     allocated to provide a more reliable way to
  19.                                     recover it when freeing.
  20.          <7>      5/4/95    jpa        Leave a min amount of free space in
  21.                                     platform heap. [1235657]
  22.          <6>    10/24/94    jpa        Strip address returned from
  23.                                     PlatformAllocateBlock. [1193659]
  24.          <5>     9/14/94    jpa        Eliminated dependencies on rest of OpenDoc.
  25.                                     [1186692]
  26.          <4>     8/17/94    jpa        Zap segments when freed, and check for
  27.                                     errors [1181509]
  28.          <3>      8/8/94    jpa        OD_DEBUG --> ODDebug
  29.          <2>     6/10/94    MB        Make it build
  30.          <1>      6/9/94    MB        first checked in
  31.          <3>     5/26/94    MB        #1162181: Fixed MMM integration bug
  32.          <2>      5/9/94    MB        #1162181: Changes necessary to install MMM.
  33.          <1>     4/29/94    MB        first checked in
  34.     To Do:
  35.     In Progress:
  36. */
  37.  
  38. #ifndef _PLATFMEM_
  39. #include "PlatfMem.h"
  40. #endif
  41.  
  42. #ifndef _MEMMGRPV_
  43. #include "MemMgrPv.h"
  44. #endif
  45.  
  46. #ifndef _MEMDEBG_
  47. #include "MemDebg.h"
  48. #endif
  49.  
  50. #ifndef __ERRORS__
  51. #include <Errors.h>
  52. #endif
  53.  
  54. #include <string.h>
  55. #include <stdio.h>
  56. #include <stdarg.h>
  57.  
  58.  
  59. //========================================================================================
  60. // Global function definitions
  61. //========================================================================================
  62.  
  63. //----------------------------------------------------------------------------------------
  64. // PlatformZapMem
  65. //----------------------------------------------------------------------------------------
  66.  
  67. void PlatformZapMem( void *start, size_t size, MMULong value )
  68. {
  69.     MM_ASSERT(((size_t)start&3)==0);                // Must start on a 4byte boundary
  70.     long *blk = (long *) start;
  71.     for (long i = size>>2; i>0; i--)                     // (i must be signed!)
  72.         *blk++ = value;
  73.     if( size&3 )
  74.         PlatformCopyMemory(&value,blk, size&3);        // Fill remaining 1-3 bytes
  75. }
  76.  
  77. //----------------------------------------------------------------------------------------
  78. // PlatformAllocateBlock
  79. //----------------------------------------------------------------------------------------
  80.  
  81. void *PlatformAllocateBlock(size_t size, MMHeapLocation src )
  82. {
  83.     size += sizeof(Handle);
  84.     
  85.     // Check total free space before trying anything; must leave some amount free.
  86.     size_t free;
  87.     MMSystemFreeSpace(src,&free,kMMNULL);
  88.     if( free-size < kPlatformMinFreeSpace )
  89.         return kMMNULL;
  90.         
  91.     Handle handle = (Handle) MMAllocateHandleIn(size,src);
  92.             
  93.     if ( handle ) {
  94.         if( src==kMMAppMemory )
  95.             ::MoveHHi(handle);    // Jeff Crawford sez we don't need MoveHHi for temp/sys heap
  96.         ::HLock(handle);
  97.         OSErr err = MemError();
  98.         
  99.         if (err == noErr) {
  100.             Handle* start = (Handle*) StripAddress(*handle);
  101.             *(start++) = handle;        // Store handle in 1st 4 bytes
  102.             return start;                // And return ptr to just past it
  103.         } else
  104.             DisposeHandle(handle);
  105.     }
  106.     
  107.     return kMMNULL;
  108. }
  109.  
  110. //----------------------------------------------------------------------------------------
  111. // PlatformFreeBlock
  112. //----------------------------------------------------------------------------------------
  113.  
  114. void PlatformFreeBlock(void *ptr)
  115. {
  116.     Handle handle = ((Handle*)ptr)[-1];    // Handle was stored just before block
  117.  
  118. #if MM_DEBUG
  119.     if( !MMValidateHandle(handle) )
  120.         return;
  121.     if( StripAddress(*handle) != StripAddress(ptr)-4 ) {
  122.         MM_WARN("PlatformFreeBlock: Hdl %p doesn't point to %p!",handle,ptr);
  123.         return;
  124.     }
  125.     // Zap the block as we dispose it:
  126.     PlatformZapMem(*handle,::GetHandleSize(handle),0xDEADBEEF);
  127. #endif
  128.  
  129.     ::DisposeHandle(handle);
  130.     
  131. #if MM_DEBUG
  132.     OSErr err = MemError();
  133.     if( err && err != memFullErr )
  134.         MM_WARN("PlatformFreeBlock got err %hd on hdl %p",err,handle);
  135. #endif
  136. }
  137.  
  138. //----------------------------------------------------------------------------------------
  139. // BREAK
  140. //----------------------------------------------------------------------------------------
  141.  
  142. static void
  143. BREAK( const char msg[] )
  144. {
  145.     if( gHaveSOM )
  146.         somPrintf("%s\n",msg);
  147.     
  148.     // Now convert to Pascal string and call DebugStr:
  149.     Str255 pstr;
  150.     long len = strlen(msg);
  151.     pstr[0] = (len>255) ?255 :len;
  152.     for( int i=pstr[0]; i>0; i-- ) {
  153.         char c = msg[i-1];
  154.         if( c==';' ) c=':';        // ";" is bad in DebugStrs
  155.         pstr[i] = c;
  156.     }
  157.     DebugStr(pstr);
  158. }
  159.  
  160.  
  161. //----------------------------------------------------------------------------------------
  162. // MM_ASSERT_FAILED
  163. //----------------------------------------------------------------------------------------
  164.  
  165. void MM_ASSERT_FAILED(const char *cond, const char *fil)
  166. {
  167.     char dbg[256];
  168.     sprintf(dbg,"MM: %s ...NOT! In %s", cond,fil); 
  169.     BREAK(dbg);
  170. }
  171.  
  172. //----------------------------------------------------------------------------------------
  173. // MM_SHOW_WARNING
  174. //----------------------------------------------------------------------------------------
  175.  
  176. void MM_SHOW_WARNING(const char *msg, ...)
  177. {
  178.     char buf[512];
  179.     strcpy(buf, "MMWarning: ");
  180.     va_list args;
  181.     va_start(args,msg);
  182.     vsprintf(buf+strlen(buf),msg,args);
  183.     va_end(args);
  184.     
  185.     BREAK(buf);
  186. }
  187.